package com.csdemos;

import com.csdemos.algs.PriorityQueue;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class PriorityQueueDemoActivity extends Activity {
	
	private PriorityQueue priorityQueue;
	
	private EditText i;
	private EditText key;
	private EditText i2;
	private TextView array;
	private TextView result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_priority_queue_demo);
		i = (EditText) findViewById(R.id.i);
		key = (EditText) findViewById(R.id.key);
		i2 = (EditText) findViewById(R.id.i2);
		
		array = (TextView) findViewById(R.id.array);
		result = (TextView) findViewById(R.id.result);
		
		priorityQueue = new PriorityQueue();
		refresh();
	}
	
	private void setResultString(int result) {
		this.result.setText(String.valueOf(result));
	}
	
	private void refresh() {
		array.setText(priorityQueue.toString());
	}
	
	public void max(View view) {
		try {
			setResultString(priorityQueue.heapMaximum());
			refresh();
		} catch (Exception e) {
			result.setText(e.getMessage());
		}
		
	}
	
	public void extractMax(View view) {
		try {
			setResultString(priorityQueue.heapExtractMax());
			refresh();
		} catch (Exception e) {
			result.setText(e.getMessage());
		}
		
	}
	
	public void increase(View view) {
		try {
			priorityQueue.heapIncreaseKey(
					Integer.valueOf(i.getText().toString()),
					Integer.valueOf(key.getText().toString()));
			refresh();
		} catch (Exception e) {
			result.setText(e.getMessage());
		}
	}
	
	public void insert(View view) {
		priorityQueue.maxHeapInsert(
				Integer.valueOf(i2.getText().toString()));
		refresh();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_priority_queue_demo, menu);
		return true;
	}

}
